DEF_SCRIPTS:=$(notdir $(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))))
DIR_PROJECT:=$(shell readlink -f $(DEF_PROJECT))
DIR_SCRIPTS=$(DIR_PROJECT)/$(DEF_SCRIPTS)
include $(DIR_SCRIPTS)/constants.mk
include $(DIR_SCRIPTS)/version.mk
include $(DIR_SCRIPTS)/cross.mk
include $(DIR_SCRIPTS)/download.mk
include $(DIR_SCRIPTS)/function.mk

PID:=$(strip $(shell ps -oppid --no-heading -p$$$$))

DIR_RELEASE:=$(DIR_PROJECT)/$(DEF_RELEASE)
DIR_TARGETS:=$(DIR_PROJECT)/$(DEF_TARGETS)
DIR_BUILD:=$(DIR_PROJECT)/$(DEF_BUILD)
DIR_SYSROOT:=$(DIR_PROJECT)/$(DEF_SYSROOT)
DIR_DOWNLOAD:=$(DIR_PROJECT)/$(DEF_DOWNLOAD)
DIR_TOOLS:=$(DIR_PROJECT)/$(DEF_TOOLS)
DIR_TARGET:=$(DIR_PROJECT)/$(DEF_TARGETS)
DIR_ORIG:=$(DIR_TARGET)/$(subst $(DIR_BUILD)/,,$(subst $(DIR_TARGET)/,,$(CURDIR)))
DIR_WORK:=$(DIR_BUILD)/$(subst $(DIR_TARGET)/,,$(DIR_ORIG))

export LD_LIBRARY_PATH:=$(DIR_TOOLS)/lib
export PATH:=$(DIR_TOOLS)/bin:$(PATH)

ifndef REV_REVISION 
REV_REVISION:=$(shell LANG= svn info | grep 'Revision' | sed 's/[^0-9]//g')
endif

ifndef CC_BUILD
CC_BUILD:=$(shell $(CC) -dumpmachine)
endif

ifndef CC_HOST
CC_HOST:=$(CC_BUILD)
endif

ifndef CC_ARCH
CC_ARCH:=$(shell cut -d- -f1<<<$(CC_BUILD))
endif
ifndef CC_MACHINE
CC_MACHINE:=$(shell cut -d- -f2<<<$(CC_BUILD))
endif
ifndef CC_OSTYPE
CC_OSTYPE:=$(shell cut -d- -f3-4<<<$(CC_BUILD))
endif

CC_TARGET:=$(CC_ARCH)-$(CC_MACHINE)-$(CC_OSTYPE)

ifndef source
source=$(name)$(addprefix -,$(version))
endif

ifndef srctree
srctree=$(source)
endif

ifndef download 
download=$(name)$(addprefix -,$(version))$(addprefix .,$(DL_SUFFIX))
endif

#ifndef suffix
#suffix=$(DL_SUFFIX)
#endif

ifndef subdir
subdir=$(addprefix $(addsuffix /,$1),$(filter-out .%,$(shell find $1 -maxdepth 1 -mindepth 1 -type d -printf '%f ')))
endif

fullname=$(name)$(addprefix -,$(dist))$(addprefix -,$(group))

CFLAGS+=$(CC_CFLAGS)

LDFLAGS+=$(CC_LDFLAGS)

cdefs+=CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)'

cflags+=--prefix=$(prefix)

iflags+=install $(if $(destdir),DESTDIR=$(destdir)) $(if $(DESTDIR),DESTDIR=$(DESTDIR))

.PHONY: depend clean distclean mostlyclean maintainer-clean
.SUFFIXES: .tar.gz .tar.bz2 .tar.xz .tgz .gz .tar .patch.gz .cvd .diff .diff.gz

vpath %.tar.gz $(DIR_DOWNLOAD)
vpath %.tar.bz2 $(DIR_DOWNLOAD)
vpath %.tar.xz $(DIR_DOWNLOAD)
vpath %.tgz $(DIR_DOWNLOAD)
vpath %.gz $(DIR_DOWNLOAD)
vpath %.tar $(DIR_DOWNLOAD)
vpath %.diff.gz $(DIR_DOWNLOAD)
vpath %.cvd $(DIR_DOWNLOAD)
vpath %.zip $(DIR_DOWNLOAD)
vpath %.diff $(DIR_ORIG)/$(DEF_PATCH)

VPATH:=$(DIR_WORK):$(DIR_ORIG)

ifneq '$(CURDIR)' '$(DIR_PROJECT)'
ifeq '$(CURDIR)' '$(DIR_ORIG)'
include $(DIR_SCRIPTS)/targets.mk
endif
ifeq '$(CURDIR)' '$(DIR_WORK)'
include $(DIR_SCRIPTS)/build.mk
endif
else
endif
